package com.store.framework.db;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Properties;

import javax.sql.DataSource;
import javax.sql.rowset.CachedRowSet;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.rowset.CachedRowSetImpl;

public class DaoUtil  {
	private static Log LOG = LogFactory.getLog(DaoUtil.class);
	private DaoUtil (){}
//	private static BasicDataSource basicDataSource;
	private static DataSource dataSource;
	
	//private static java.sql.Connection connection;
//	public static BasicDataSource getBasicDataSource() throws SQLException{
//    	if(basicDataSource == null){
//    		basicDataSource = new BasicDataSource();
////    		ResourceBundle props = ResourceBundle.getBundle("jdbc");
//    		basicDataSource.setDriverClassName(JdbcUtil.getValue(JdbcUtil.DB_DRIVER));
//    		basicDataSource.setUrl(JdbcUtil.getValue(JdbcUtil.DB_URL));
//	    	basicDataSource.setUsername(JdbcUtil.getValue(JdbcUtil.DB_USER));
//	    	basicDataSource.setPassword(JdbcUtil.getValue(JdbcUtil.DB_PASSWORD));
//	    	basicDataSource.setMaxActive(Integer.parseInt((JdbcUtil.getValue(JdbcUtil.DB_MAXCONNECTION))));
//	    	basicDataSource.setMaxIdle(10);
//	    	basicDataSource.setMaxWait(3000);
//	    	basicDataSource.setDefaultAutoCommit(false);
//    	}
	static{
		try {
			// * 创建
			Properties props = new Properties();
			
			InputStream is = DaoUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
			
			props.load(is);
			
			dataSource = BasicDataSourceFactory.createDataSource(props);
			
		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
		}
    	//return basicDataSource;
    }
	
	public static CachedRowSet SimpleQuery(String execSql,Object ... params) throws SQLException{
		Connection conn = getConnection();
		LOG.info(String.format("exec the sql [%s],time: [%s]", execSql,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date())));
		PreparedStatement ps = conn.prepareStatement(execSql);
		for(int i = 0;i<params.length;i++){
			ps.setObject(i+1, params[i]);
		}
		ResultSet rs = ps.executeQuery();
		CachedRowSet crs = new CachedRowSetImpl(); 
		crs.populate(rs); 
		rs.close();
		conn.close();
		return crs;
	}
	
	public static int executeUpdate(String execSql,Object ... params) throws SQLException{
		Connection conn = getConnection();
		conn.setAutoCommit(false);
		LOG.info(String.format("exec the sql [%s],time: [%s]", execSql,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date())));
		PreparedStatement ps = conn.prepareStatement(execSql);
		for(int i = 0;i<params.length;i++){
			ps.setObject(i+1, params[i]);
		}
		int row= ps.executeUpdate();
		conn.commit();
		conn.close();
		return row;
	}
	
	private static Connection getConnection() throws SQLException{
		Connection conn = dataSource.getConnection();
		return conn;
	}
}








